//
//  EBImageBrowser.m
//  test
//
//  Created by 李彦宏 on 2018/3/23.
//  Copyright © 2018年 朱圳. All rights reserved.
//点击图片放大

#import "EBImageBrowser.h"

@implementation EBImageBrowser
static CGRect oldframe;

+ (void)showImage:(UIImageView *)avatarImageView{
UIImage *image = avatarImageView.image;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
oldframe = [avatarImageView convertRect:avatarImageView.bounds toView:window];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.alpha = 0;
       UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldframe];
imageView.image = image;
imageView.userInteractionEnabled=YES;
imageView.multipleTouchEnabled=YES;

imageView.tag = 1;
        [backgroundView addSubview:imageView];
[window addSubview:backgroundView];
    //点击退出全屏
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
[backgroundView addGestureRecognizer: tap];
  //缩放
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [imageView addGestureRecognizer:pinchGestureRecognizer];
 //旋转
    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)];
    [imageView addGestureRecognizer:rotationGestureRecognizer];
// //拖动
//    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];
//  
//    [imageView addGestureRecognizer:panGestureRecognizer];
    
    
[UIView animateWithDuration:0.2 animations:^{
               imageView.frame = CGRectMake(0,([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) / 2, [UIScreen mainScreen].bounds.size.width, image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width);
                 backgroundView.alpha = 1;
             } completion:^(BOOL finished) {
        
                 }];
     }
//点击退出全屏
 +(void)hideImage:(UITapGestureRecognizer *)tap{
         UIView *backgroundView = tap.view;
         UIImageView *imageView=(UIImageView *)[tap.view viewWithTag:1];
         [UIView animateWithDuration:0.2 animations:^{
                 imageView.frame = oldframe;
                 backgroundView.alpha = 0;
             } completion:^(BOOL finished) {
                    [backgroundView removeFromSuperview];
                 }];
     }
// 处理缩放手势
+(void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    NSLog(@"11121341");
    UIView *view = pinchGestureRecognizer.view;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        pinchGestureRecognizer.scale = 1;
    }
}
// //拖动
//+(void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
//{
//    UIGestureRecognizerState state = [recognizer state];
//
//    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
//    {
//        CGPoint translation = [recognizer translationInView:recognizer.view];
//        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
//        [recognizer setTranslation:CGPointZero inView:recognizer.view];
//    }
//}
 //旋转
+(void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer
{
    UIGestureRecognizerState state = [recognizer state];
    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGFloat rotation = [recognizer rotation];
        [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)];
        [recognizer setRotation:0];
    }
}
@end
